package com.szrt.day20170420.ch02;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

public class HashMapTest {
	
	
	
	@Test
	public void hashMapTest05() throws Exception {
		
		Map<Long,Person> map = new HashMap<Long,Person>();
		
		//电话本    张三  {13933334444,"男"}
		//电话本    李四  {13933334455,"男"}
		//电话本    小芳  {13988889999,"女"}
		
		
		ContactPerson zs = new ContactPerson(13933334444L,"男","张三");
		ContactPerson li = new ContactPerson(13933334455L,"男","李四");
		ContactPerson xf = new ContactPerson(13988889999L,"女","小芳");
		
		
		
		
		//key value 键值对  keySet {13933334444L,13933334455L,13988889999L}
		map.put(13933334444L, zs);
		map.put(li.getPhone(), li);
		map.put(xf.getPhone(), xf);
		
		
		//把李四的手机号码修改成 13933334466L 指针
		Person ls = map.get(13933334455L);
		ls.setPhone(13933334466L);
		
		
		Set<Long> keySet = map.keySet();
		
		Iterator<Long> iterator = keySet.iterator();
		
		while(iterator.hasNext()){
			Long key = iterator.next();
			
			Person person = map.get(key);
			
			System.out.println(person);
		}
		
		
		
		
		
		
		
		
	}
	
	
	/**
	 * class1704.get("罗智壕");
	 * 第4层心法
	 * @throws Exception
	 */
	
	
	@Test
	public void hashMapTest04() throws Exception {
		
		Map<String,Person> map = new HashMap<String,Person>();
		
		//电话本    张三  {13933334444,"男"}
		//电话本    李四  {13933334455,"男"}
		//电话本    小芳  {13988889999,"女"}
		
		
		ContactPerson zs = new ContactPerson(13933334444L,"男");
		ContactPerson li = new ContactPerson(13933334455L,"男");
		ContactPerson xf = new ContactPerson(13988889999l,"女");
		
		
		
		
		//key value 键值对
		map.put("张三", zs);
		map.put("李四", li);
		map.put("小芳", xf);
		
		//拿 小芳
		
		Person person = map.get("小芳");
		System.out.println(person);
		
		//拿出来有没有顺序  
		
		
		//{"张三","李四","小芳"}  keySet
		
		Set<String> keySet = map.keySet();
		
		Iterator<String> iterator = keySet.iterator();
		
		while(iterator.hasNext()){
			String name = iterator.next();
			Person cp =map.get(name);
			System.out.println(name +" : " + cp);
		}
		
		
		
		
	}
	
	
	/**
	 * 第3层心法
	 * @throws Exception
	 */
	@Test
	public void hashMapTest03() throws Exception {
		
		Map<String,ContactPerson> map = new HashMap<String,ContactPerson>();
		
		//电话本    张三  {13933334444,"男"}
		//电话本    李四  {13933334455,"男"}
		//电话本    小芳  {13988889999,"女"}
		
		
		ContactPerson zs = new ContactPerson(13933334444L,"男");
		ContactPerson li = new ContactPerson(13933334455L,"男");
		ContactPerson xf = new ContactPerson(13988889999l,"女");
		
		map.put("张三", zs);
		map.put("李四", li);
		map.put("小芳", xf);
		
		//拿 小芳
		
		ContactPerson contactPerson = map.get("小芳");
		System.out.println(contactPerson);
		
		//拿出来有没有顺序  
		
		
		//{"张三","李四","小芳"}  keySet
		
		Set<String> keySet = map.keySet();
		
		Iterator<String> iterator = keySet.iterator();
		
		while(iterator.hasNext()){
			String name = iterator.next();
			
			ContactPerson cp =map.get(name);
			System.out.println(name +" : " + cp);
		}
		
		
		
		
	}
	
	
	/**
	 * 第2层心法
	 * @throws Exception
	 */
	@Test
	public void hashMapTest02() throws Exception {
		
		Map<String,Long> map = new HashMap<String,Long>();
		
		//电话本    张三  {13933334444,"男"}
		//电话本    李四  {13933334455,"男"}
		//电话本    小芳  {13988889999,"男"}
		
		map.put("张三", 13933334444L);
		map.put("李四", 13933334455L);
		
		
		
		//查询李四的电话号码  相当于字典前面偏旁部首==》索引  可以快速查找
		
		Long object = map.get("李四");
		System.out.println(object);
		
		
		
		
	}
	/**
	 * 第1层心法
	 * @throws Exception
	 */
	@Test
	public void hashMapTest01() throws Exception {
		
		Map map = new HashMap();
		
		//电话本    张三  13933334444
		//电话本    李四  13933334455
		
		map.put("张三", 13933334444L);
		map.put("李四", 13933334455L);
		
		
		
		//查询李四的电话号码  相当于字典前面偏旁部首==》索引  可以快速查找
		
		Object object = map.get("李四");
		System.out.println(object);
		
		
		
		
	}

}
